package com.xcl.service.impl;

import com.xcl.mapper.CentreMapper;
import com.xcl.mapper.XclBreakmajorityEpibolyMapper;
import com.xcl.mapper.XclLogisticsMapper;
import com.xcl.mapper.XclWaybillMapper;
import com.xcl.pojo.BreakmajorityEpiboly;
import com.xcl.service.XclBreakmajorityEpibolyServiceInter;
import com.zzg.entity.*;
import com.zzg.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName XclBreakmajorityEpibolyServiceImpl
 * @Description 零担外服务层实现类
 * @Author: xiaochaolei
 * @date 2021/7/16 10:06
 * @Version 1. 0
 */
@Service
public class XclBreakmajorityEpibolyServiceImpl implements XclBreakmajorityEpibolyServiceInter {
    @Autowired
    XclBreakmajorityEpibolyMapper xclBreakmajorityEpibolyMapper;

    @Autowired
    XclLogisticsMapper xclLogisticsMapper;

    @Autowired
    XclWaybillMapper xclWaybillMapper;

    @Autowired
    CentreMapper centreMapper;

    @Override
    public List<BreakmajorityEpiboly> getBreakmajorityEpibolyAll() {
        return xclBreakmajorityEpibolyMapper.getBreakmajorityEpibolyAll();
    }

    @Override
    public List<BreakmajorityEpiboly> getBreakmajorityEpibolyByLimit(int page, int limit) {
        return xclBreakmajorityEpibolyMapper.getBreakmajorityEpibolyByLimit((page-1)*limit,limit);
    }

    @Override
    public List<BreakmajorityEpiboly> getBreakmajorityEpibolyByWhere(int page, int limit, String sub_id, String waybill_id, String arrivalCity, String consignDate) {
        return xclBreakmajorityEpibolyMapper.getBreakmajorityEpibolyByWhere((page-1)*limit,limit,sub_id,waybill_id,arrivalCity,consignDate);
    }

    @Override
    public BreakmajorityEpiboly getBreakmajorityEpibolyById(String id) {
        return xclBreakmajorityEpibolyMapper.getBreakmajorityEpibolyById(id);
    }

    @Override
    public List<String> getLogisticsAll() {
        List<String> list=new ArrayList<>();
        List<Logistics> allConsigneeByClientname = xclBreakmajorityEpibolyMapper.getLogisticsAll();
        for (int i = 0; i < allConsigneeByClientname.size(); i++) {
            list.add( allConsigneeByClientname.get(i).getForwardingunit()+"(编号"+ allConsigneeByClientname.get(i).getLogisticsId()+")");
        }
        return list;
    }

    @Override
    public Logistics getLogisticsById(String id) {
        return xclBreakmajorityEpibolyMapper.getLogisticsById(id);
    }

    @Override
    public Outsourcing getOutsourcingByOutsourcingOrder(double outsourcingOrder) {
        return xclBreakmajorityEpibolyMapper.getOutsourcingByOutsourcingOrder(outsourcingOrder);
    }

    @Override
    public int insertElseRpiboly(Logistics logistics, Outsourcing outsourcing, String location,String id) {
        //得到物流承担上的电话号码
        String phone=logistics.getPhone();
        //定义类型存储物流承担商id
        String  logisticsId="";
        Logistics logisticsByPhone = xclBreakmajorityEpibolyMapper.getLogisticsByPhone(phone);
        if (logisticsByPhone==null){
            System.out.println("新增");
            //设置id
            logistics.setLogisticsId(UuidUtil.getUuid());
            //判断地区是存在
            String[] split = location.split("-");
            //存储地区id
            String regionId="";
            if (split.length>1){
                //实例化地区(做新增操作)
                TmsRegion tmsRegion=new TmsRegion();
                //设置id
                tmsRegion.setRegionId(UuidUtil.getUuid());
                //设置省
                tmsRegion.setProvince(split[0]);
                //设置市
                tmsRegion.setCity(split[1]);
                //设置区
                tmsRegion.setDistrict(split[2]);
                //设置详细地址
                tmsRegion.setDetailedAddress(split[0]+split[1]+split[2]+ split[3]);
                //得到id
                regionId=tmsRegion.getRegionId();
                //新增地区
                xclWaybillMapper.insertTmsRegion(tmsRegion);
            }
            //设置物流承担商的地区id
            logistics.setRegionId(regionId);
            //设置外包中的物流承担商
            outsourcing.setLogisticsId(logistics.getLogisticsId());
            //新增物流承担商
            xclLogisticsMapper.insertLogistics(logistics);
        }else {
            //设置物流承担商
            outsourcing.setLogisticsId(logisticsByPhone.getLogisticsId());
        }
        Waybill waybillById = xclBreakmajorityEpibolyMapper.getWaybillById(id);
        //新增外包信息
        outsourcing.setFreightinformation(waybillById.getFreightinformation());
        //设置主键id
        outsourcing.setOutsourcingId(UuidUtil.getUuid());
        //新增外包
        xclBreakmajorityEpibolyMapper.insertOutsourcing(outsourcing);
        //将状态修改外运输
        xclBreakmajorityEpibolyMapper.updateBreakmajorityEpiboly(id);

        //新增财务详细表的信息
        //实例化财务详细模型类
        Costbreakdown costbreakdown=new Costbreakdown();
        //设置财务详细的id
        costbreakdown.setCostbreakdownId(UuidUtil.getUuid());
        //应该收取的费用
        costbreakdown.setReceivables(outsourcing.getOutsourcingNowclosed());
        //已经收取的费用
        costbreakdown.setRealizefee(outsourcing.getRecycle());
        //未结清的费用
        costbreakdown.setOwefee(costbreakdown.getReceivables()-costbreakdown.getRealizefee());
        //新增财务详细
        centreMapper.insertCostbreakdown(costbreakdown);



       //实例化财务模型类
        Finance financing=new Finance();
        //设置财务的财务详细id
        financing.setCostbreakdownId(costbreakdown.getCostbreakdownId());
        //设置财务模型类的id（外包模型类的id）
        financing.setOrdernumber(outsourcing.getOutsourcingId());
        //设置id
        financing.setFinance(UuidUtil.getUuid());
        //根据值设置财务状态
        if (Double.doubleToLongBits(costbreakdown.getReceivables())==Double.doubleToLongBits(costbreakdown.getRealizefee())){
            //运单状态为已结清
            financing.setOrderstatus("已结清");
        }else if (Double.doubleToLongBits(costbreakdown.getReceivables())>Double.doubleToLongBits(costbreakdown.getRealizefee())){
            //运单状态为未结清
            financing.setOrderstatus("未结清");
        }else{
            //运单状态为未付款
            financing.setOrderstatus("未付款");
        }
        //设置财务类型
        financing.setOrdertype(outsourcing.getType());
        //新增财务表
        centreMapper.insertFinance(financing);


        return 1;
    }
}
